package com.eee.xiang.market.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eee.xiang.market.beans.*;
import com.eee.xiang.market.common.TableData;
import com.eee.xiang.market.dao.mapper.TOrderDetailMapper;
import com.eee.xiang.market.dao.mapper.TOrderMapper;
import com.eee.xiang.market.service.AbstractOrderService;
import com.eee.xiang.market.service.IOrderService;
import com.eee.xiang.market.utils.ApplicationUtil;
import com.eee.xiang.market.utils.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private TOrderDetailMapper orderDetailMapper;

    @Autowired
    private TOrderMapper orderMapper;

    @Autowired
    private HttpSession session;

    @Override
    @Transactional
    public OperateResponse makeOrder(String orderStr) {
        JSONObject jsonObject = JSON.parseObject(orderStr);
        String orderId = UUIDUtil.generate();
        try {
            TUser staff = (TUser) session.getAttribute("userInfo");
            BigDecimal totalAmount = jsonObject.getBigDecimal("totalAmount");
            TOrder cargoOrder = new TOrder();
            cargoOrder.setOrderId(orderId);
            cargoOrder.setStaffId(staff.getUserId());
            cargoOrder.setTotalAmount(totalAmount);
            cargoOrder.setStatus((byte)1);
            orderMapper.insertSelective(cargoOrder);
            JSONArray orders = jsonObject.getJSONArray("orders");
            for(Object order : orders){
                TOrderDetail orderDetail = new TOrderDetail();
                JSONObject orderEntity = (JSONObject) order;
                String cargoId = orderEntity.getString("cargoId");
                int buyNum = orderEntity.getIntValue("buyNum");
                BigDecimal amount = orderEntity.getBigDecimal("amount");
                orderDetail.setDetailId(UUIDUtil.generate());
                orderDetail.setCargoId(cargoId);
                orderDetail.setAmount(amount);
                orderDetail.setCargoNum(buyNum);
                orderDetail.setOrderId(orderId);
                orderDetailMapper.insertSelective(orderDetail);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return OperateResponse.failResponse("提交订单失败！");
        }
        return OperateResponse.obtainSuccessResponse(true);
    }

    @Override
    public OperateResponse getAllOrder() {
        List<TOrder> tOrders = orderMapper.selectByExample(null);
        return OperateResponse.obtainSuccessResponse(tOrders);
    }

    @Override
    public OperateResponse getAllOrderDetail() {
        TOrderDetailExample example = new TOrderDetailExample();
        example.setOrderByClause("create_datetime desc");
        List<TOrderDetail> tOrders = orderDetailMapper.selectByExample(example);
        return OperateResponse.obtainSuccessResponse(tOrders);
    }

    @Override
    public OperateResponse getAllOrderByStaffId(String staffId) {
        List<TOrder> allOrderByStaffId = orderMapper.getAllOrderByStaffId(staffId);
        return OperateResponse.obtainSuccessResponse(allOrderByStaffId);
    }

    @Override
    public OperateResponse getOneCargoOrder(String cargoId) {
        TOrderDetailExample example = new TOrderDetailExample();
        example.createCriteria().andCargoIdEqualTo(cargoId);
        List<TOrderDetail> tOrderDetails = orderDetailMapper.selectByExample(example);
        return OperateResponse.obtainSuccessResponse(tOrderDetails);
    }

    @Override
    public OperateResponse getCargoSellSituation() {
        List<TOrderDetail> tOrders = orderDetailMapper.selectByExample(null);
        Map<String, List<TOrderDetail>> collect = tOrders.stream().collect(Collectors.groupingBy(TOrderDetail::getCargoId));
        return OperateResponse.obtainSuccessResponse(collect);
    }

    @Override
    public TableData getAllOrderList(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<TOrder> tOrders = orderMapper.selectByExample(null);
        return new TableData(tOrders.size(),tOrders);
    }

    @Override
    public TableData getAllOrderDetailList(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<TOrderDetail> tOrders = orderDetailMapper.getAllOrderDetailList();
        return new TableData(tOrders.size(),tOrders);
    }

    @Override
    public OperateResponse orderListEchart() {
        List<Map<String, Object>> maps = orderMapper.orderListEchart();
        return OperateResponse.obtainSuccessResponse(maps);
    }

    @Override
    public OperateResponse orderDetailEchart() {
        List<Map<String, Object>> maps = orderMapper.orderDetailEchart();
        return OperateResponse.obtainSuccessResponse(maps);
    }

    @Override
    public OperateResponse getOrder(String orderId) {
        return null;
    }

    @Override
    public void saveOrderDetail(String orderStr) {

    }

    @Override
    public OperateResponse testAbstract(String type) {
        AbstractOrderService bean = ApplicationUtil.getBean(type);
        bean.open();
        return OperateResponse.obtainSuccessResponse("智商绝知此事");
    }
}
